#!/bin/bash

#FILENAME="DE_KDG_TVA_Lineups_20130411.xml"
FILENAME=$1

Y_HEADEND_IDS=`xml sel -N x="urn:tva:metadata:2010" -t -m "//x:TransmissionEntity[@Device_Type_code='Y']" -v "concat(@id, ' ')" -n $1 |perl -pe 's/srid:\/\/bds.tv\/(\d+)/*$1*/' |tr "*" "'"`

P_HEADEND_IDS=`xml sel -N x="urn:tva:metadata:2010" -t -m "//x:TransmissionEntity[@Device_Type_code='P']" -v "concat(@id, ' ')" -n $1 |perl -pe 's/srid:\/\/bds.tv\/(\d+)/*$1*/' |tr "*" "'"`

X_HEADEND_IDS=`xml sel -N x="urn:tva:metadata:2010" -t -m "//x:TransmissionEntity[@Device_Type_code='X']" -v "concat(@id, ' ')" -n $1 |perl -pe 's/srid:\/\/bds.tv\/(\d+)/*$1*/' |tr "*" "'"`

# R type headend must not exist.
R_HEADEND_IDS=`xml sel -N x="urn:tva:metadata:2010" -t -m "//x:TransmissionEntity[@Device_Type_code='R']" -v "concat(@id, ' ')" -n $1 |perl -pe 's/srid:\/\/bds.tv\/(\d+)/*$1*/' |tr "*" "'"`

# Headend information
xml sel -N x="urn:tva:metadata:2010" -t -m "//x:TransmissionEntity" -v "concat('|', @id, '|', @Device_Type_code, ' |', x:Title, '|', x:OtherIdentifier[@type='MSO_Name'], '|', x:OtherIdentifier[@type='MSO_ID'], '|', x:OtherIdentifier[@type='MSO_Type'],'|', x:OtherIdentifier[@type='Device_Type'])" -n $1 > $1.tmp1

# Zip code mapping information
xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection" -v "concat(@id, ' |Sof: ', x:ServiceOf/@crid, ' |Device_Type_code: ', x:ServiceOf/@Device_Type_code, ' |Channel#: ')" -m "x:Services" -v "count(x:Service)" -b -n $1 > $1.tmp2

xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection" -v "concat(@id, ' |Geo#: ')" -m "x:GeoLocations" -v "count(x:GeoLocation)" -b -n $1 > $1.tmp3

join $1.tmp2 $1.tmp3

xml sel -N x="urn:tva:metadata:2010" -t -m "//x:TransmissionEntity" -v "concat('|', @id, '|', @Device_Type_code, ' |')" -m "x:GeoLocations" -v "count(x:GeoLocation)" -o "|" -b -n $1 > $1.tmp4

echo "||headend id||Device Code||headend name||MSO name||MSO id||MSO Type||Device Type||Zip Codes#||"

join $1.tmp1 $1.tmp4 | perl -pe 's/srid:\/\/bds.tv\///g' |sort -t "|" -k3,3

for HID in $Y_HEADEND_IDS
do
  DEV_TYPE="'Y'"
  echo $HID "DTT($DEV_TYPE Type) ---------"
  TRIPLETS=`xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection[contains(x:ServiceOf/@crid,$HID) and contains(x:ServiceOf/@Device_Type_code,$DEV_TYPE)]/x:Services" -m "x:Service" -s A:N:U x:OtherIdentifier -v "concat(ancestor::x:ServiceCollection/x:ServiceOf/@crid, '|', ancestor::x:ServiceCollection/x:ServiceOf/@Device_Type_code, '|', x:OtherIdentifier[@type='LCN'],  '|', x:OtherIdentifier[@type='DVB_ONID'], '|', x:OtherIdentifier[@type='DVB_transportStream_ID'], '|', x:OtherIdentifier[@type='DVB_service_id'], '|', ancestor::x:ServiceCollection/x:OtherIdentifier[@type='DVB_frequency'], '|', @id, '|Name:', x:Name)" -n $FILENAME | perl -pe 's/Name:\n/Name:/g' | perl -pe 's/Name:\s*/Name:/g' | perl -pe 's/^\s*\n//' | perl -pe 's/Name://g' |perl -pe 's/srid:\/\/bds.tv\///g' |sort -k3,3 -n |awk -F "|" '{print $4 ":" $5 ":" $6 ":" $7}' |sort |uniq -d |awk -F ":" '{print $1 ":" $2 ":" $3 ":" $4}'`

  echo $TRIPLETS

  xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection[contains(x:ServiceOf/@crid,$HID) and contains(x:ServiceOf/@Device_Type_code,$DEV_TYPE)]/x:Services" -m "x:Service" -s A:N:U x:OtherIdentifier -v "concat(ancestor::x:ServiceCollection/x:ServiceOf/@crid, '|', ancestor::x:ServiceCollection/x:ServiceOf/@Device_Type_code, '|', x:OtherIdentifier[@type='LCN'],  '|', x:OtherIdentifier[@type='DVB_ONID'], '|', x:OtherIdentifier[@type='DVB_transportStream_ID'], '|', x:OtherIdentifier[@type='DVB_service_id'], '|', ancestor::x:ServiceCollection/x:OtherIdentifier[@type='DVB_frequency'], '|', @id, '|Name:', x:Name)" -n $FILENAME | perl -pe 's/Name:\n/Name:/g' | perl -pe 's/Name:\s*/Name:/g' | perl -pe 's/^\s*\n//' | perl -pe 's/Name://g' |perl -pe 's/srid:\/\/bds.tv\///g' |sort -k3,3 -n > $1.tmp3

  for TRIPLET in $TRIPLETS
  do
    ONID=`echo $TRIPLET | awk -F ":" '{print $1}'`
    TSID=`echo $TRIPLET | awk -F ":" '{print $2}'`
    SID=`echo $TRIPLET | awk -F ":" '{print $3}'`
    FREQ=`echo $TRIPLET | awk -F ":" '{print $4}'`
    echo "Headend id: " $HID "ONID:" $ONID "TSID:" $TSID "SID:" $SID "FREQ:" $FREQ " Detail ---------"
    cat $1.tmp3 | grep '|'$ONID'|'$TSID'|'$SID'|'$FREQ'|'
  done
done

for HID in $P_HEADEND_IDS
do
  DEV_TYPE="'P'"
  echo $HID "Tuner($DEV_TYPE Type) ---------"
  TRIPLETS=`xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection[contains(x:ServiceOf/@crid,$HID) and contains(x:ServiceOf/@Device_Type_code,$DEV_TYPE)]/x:Services" -m "x:Service" -s A:N:U x:OtherIdentifier -v "concat(ancestor::x:ServiceCollection/x:ServiceOf/@crid, '|', ancestor::x:ServiceCollection/x:ServiceOf/@Device_Type_code, '|', x:OtherIdentifier[@type='LCN'],  '|', x:OtherIdentifier[@type='DVB_ONID'], '|', x:OtherIdentifier[@type='DVB_transportStream_ID'], '|', x:OtherIdentifier[@type='DVB_service_id'], '|', ancestor::x:ServiceCollection/x:OtherIdentifier[@type='DVB_frequency'], '|', @id, '|Name:', x:Name)" -n $FILENAME | perl -pe 's/Name:\n/Name:/g' | perl -pe 's/Name:\s*/Name:/g' | perl -pe 's/^\s*\n//' | perl -pe 's/Name://g' |perl -pe 's/srid:\/\/bds.tv\///g' |sort -k3,3 -n |awk -F "|" '{print $4 ":" $5 ":" $6 ":" $7}' |sort |uniq -d |awk -F ":" '{print $1 ":" $2 ":" $3 ":" $4}'`

  echo $TRIPLETS

  xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection[contains(x:ServiceOf/@crid,$HID) and contains(x:ServiceOf/@Device_Type_code,$DEV_TYPE)]/x:Services" -m "x:Service" -s A:N:U x:OtherIdentifier -v "concat(ancestor::x:ServiceCollection/x:ServiceOf/@crid, '|', ancestor::x:ServiceCollection/x:ServiceOf/@Device_Type_code, '|', x:OtherIdentifier[@type='LCN'],  '|', x:OtherIdentifier[@type='DVB_ONID'], '|', x:OtherIdentifier[@type='DVB_transportStream_ID'], '|', x:OtherIdentifier[@type='DVB_service_id'], '|', ancestor::x:ServiceCollection/x:OtherIdentifier[@type='DVB_frequency'], '|', @id, '|Name:', x:Name)" -n $FILENAME | perl -pe 's/Name:\n/Name:/g' | perl -pe 's/Name:\s*/Name:/g' | perl -pe 's/^\s*\n//' | perl -pe 's/Name://g' |perl -pe 's/srid:\/\/bds.tv\///g' |sort -k3,3 -n > $1.tmp3

  for TRIPLET in $TRIPLETS
  do
    ONID=`echo $TRIPLET | awk -F ":" '{print $1}'`
    TSID=`echo $TRIPLET | awk -F ":" '{print $2}'`
    SID=`echo $TRIPLET | awk -F ":" '{print $3}'`
    FREQ=`echo $TRIPLET | awk -F ":" '{print $4}'`
    echo "Headend id: " $HID "ONID:" $ONID "TSID:" $TSID "SID:" $SID "FREQ:" $FREQ " Detail ---------"
    cat $1.tmp3 | grep '|'$ONID'|'$TSID'|'$SID'|'$FREQ'|'
  done
done

for HID in $X_HEADEND_IDS
do
  DEV_TYPE="'X'"
  echo $HID "STB $DEV_TYPE Type: ---------"
  LCNS=`xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection[contains(x:ServiceOf/@crid,$HID) and contains(x:ServiceOf/@Device_Type_code,$DEV_TYPE)]/x:Services" -m "x:Service" -s A:N:U x:OtherIdentifier -v "concat(ancestor::x:ServiceCollection/x:ServiceOf/@crid, '|', ancestor::x:ServiceCollection/x:ServiceOf/@Device_Type_code, '|', x:OtherIdentifier[@type='LCN'],  '|', x:OtherIdentifier[@type='DVB_ONID'], '|', x:OtherIdentifier[@type='DVB_transportStream_ID'], '|', x:OtherIdentifier[@type='DVB_service_id'], '|', ancestor::x:ServiceCollection/x:OtherIdentifier[@type='DVB_frequency'], '|', @id, '|Name:', x:Name)" -n $FILENAME | perl -pe 's/Name:\n/Name:/g' | perl -pe 's/Name:\s*/Name:/g' | perl -pe 's/^\s*\n//' | perl -pe 's/Name://g' |perl -pe 's/srid:\/\/bds.tv\///g' |sort -k3,3 -n |awk -F "|" '{print $3}' |sort |uniq -d`

  echo $LCNS

  xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection[contains(x:ServiceOf/@crid,$HID) and contains(x:ServiceOf/@Device_Type_code,$DEV_TYPE)]/x:Services" -m "x:Service" -s A:N:U x:OtherIdentifier -v "concat(ancestor::x:ServiceCollection/x:ServiceOf/@crid, '|', ancestor::x:ServiceCollection/x:ServiceOf/@Device_Type_code, '|', x:OtherIdentifier[@type='LCN'],  '|', x:OtherIdentifier[@type='DVB_ONID'], '|', x:OtherIdentifier[@type='DVB_transportStream_ID'], '|', x:OtherIdentifier[@type='DVB_service_id'], '|', ancestor::x:ServiceCollection/x:OtherIdentifier[@type='DVB_frequency'], '|', @id, '|Name:', x:Name)" -n $FILENAME | perl -pe 's/Name:\n/Name:/g' | perl -pe 's/Name:\s*/Name:/g' | perl -pe 's/^\s*\n//' | perl -pe 's/Name://g' |perl -pe 's/srid:\/\/bds.tv\///g' |sort -k3,3 -n > $1.tmp4

  for LCN in $LCNS
  do
    echo "Headend id: " $HID "LCN: " $LCN "Detail ---------"
    cat $1.tmp4 | grep '|'$LCN'|'
  done
done

for HID in $R_HEADEND_IDS
do
  DEV_TYPE="'R'"
  echo $HID "Tuner($DEV_TYPE Type) ---------"
  TRIPLETS=`xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection[contains(x:ServiceOf/@crid,$HID) and contains(x:ServiceOf/@Device_Type_code,$DEV_TYPE)]/x:Services" -m "x:Service" -s A:N:U x:OtherIdentifier -v "concat(ancestor::x:ServiceCollection/x:ServiceOf/@crid, '|', ancestor::x:ServiceCollection/x:ServiceOf/@Device_Type_code, '|', x:OtherIdentifier[@type='LCN'],  '|', x:OtherIdentifier[@type='DVB_ONID'], '|', x:OtherIdentifier[@type='DVB_transportStream_ID'], '|', x:OtherIdentifier[@type='DVB_service_id'], '|', ancestor::x:ServiceCollection/x:OtherIdentifier[@type='DVB_frequency'], '|', @id, '|Name:', x:Name)" -n $FILENAME | perl -pe 's/Name:\n/Name:/g' | perl -pe 's/Name:\s*/Name:/g' | perl -pe 's/^\s*\n//' | perl -pe 's/Name://g' |perl -pe 's/srid:\/\/bds.tv\///g' |sort -k3,3 -n |awk -F "|" '{print $4 ":" $5 ":" $6 ":" $7}' |sort |uniq -d |awk -F ":" '{print $1 ":" $2 ":" $3 ":" $4}'`

  echo $TRIPLETS

  xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection[contains(x:ServiceOf/@crid,$HID) and contains(x:ServiceOf/@Device_Type_code,$DEV_TYPE)]/x:Services" -m "x:Service" -s A:N:U x:OtherIdentifier -v "concat(ancestor::x:ServiceCollection/x:ServiceOf/@crid, '|', ancestor::x:ServiceCollection/x:ServiceOf/@Device_Type_code, '|', x:OtherIdentifier[@type='LCN'],  '|', x:OtherIdentifier[@type='DVB_ONID'], '|', x:OtherIdentifier[@type='DVB_transportStream_ID'], '|', x:OtherIdentifier[@type='DVB_service_id'], '|', ancestor::x:ServiceCollection/x:OtherIdentifier[@type='DVB_frequency'], '|', @id, '|Name:', x:Name)" -n $FILENAME | perl -pe 's/Name:\n/Name:/g' | perl -pe 's/Name:\s*/Name:/g' | perl -pe 's/^\s*\n//' | perl -pe 's/Name://g' |perl -pe 's/srid:\/\/bds.tv\///g' |sort -k3,3 -n > $1.tmp3

  for TRIPLET in $TRIPLETS
  do
    ONID=`echo $TRIPLET | awk -F ":" '{print $1}'`
    TSID=`echo $TRIPLET | awk -F ":" '{print $2}'`
    SID=`echo $TRIPLET | awk -F ":" '{print $3}'`
    FREQ=`echo $TRIPLET | awk -F ":" '{print $4}'`
    echo "Headend id: " $HID "ONID:" $ONID "TSID:" $TSID "SID:" $SID "FREQ:" $FREQ " Detail ---------"
    cat $1.tmp3 | grep '|'$ONID'|'$TSID'|'$SID'|'$FREQ'|'
  done
done

for HID in $R_HEADEND_IDS
do
  DEV_TYPE="'R'"
  echo $HID "STB($DEV_TYPE Type) ---------"
  LCNS=`xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection[contains(x:ServiceOf/@crid,$HID) and contains(x:ServiceOf/@Device_Type_code,$DEV_TYPE)]/x:Services" -m "x:Service" -s A:N:U x:OtherIdentifier -v "concat(ancestor::x:ServiceCollection/x:ServiceOf/@crid, '|', ancestor::x:ServiceCollection/x:ServiceOf/@Device_Type_code, '|', x:OtherIdentifier[@type='LCN'],  '|', x:OtherIdentifier[@type='DVB_ONID'], '|', x:OtherIdentifier[@type='DVB_transportStream_ID'], '|', x:OtherIdentifier[@type='DVB_service_id'], '|', ancestor::x:ServiceCollection/x:OtherIdentifier[@type='DVB_frequency'], '|', @id, '|Name:', x:Name)" -n $FILENAME | perl -pe 's/Name:\n/Name:/g' | perl -pe 's/Name:\s*/Name:/g' | perl -pe 's/^\s*\n//' | perl -pe 's/Name://g' |perl -pe 's/srid:\/\/bds.tv\///g' |sort -k3,3 -n |awk -F "|" '{print $3}' |sort |uniq -d`

  echo $LCNS

  xml sel -N x="urn:tva:metadata:2010" -t -m "//x:ServiceCollection[contains(x:ServiceOf/@crid,$HID) and contains(x:ServiceOf/@Device_Type_code,$DEV_TYPE)]/x:Services" -m "x:Service" -s A:N:U x:OtherIdentifier -v "concat(ancestor::x:ServiceCollection/x:ServiceOf/@crid, '|', ancestor::x:ServiceCollection/x:ServiceOf/@Device_Type_code, '|', x:OtherIdentifier[@type='LCN'],  '|', x:OtherIdentifier[@type='DVB_ONID'], '|', x:OtherIdentifier[@type='DVB_transportStream_ID'], '|', x:OtherIdentifier[@type='DVB_service_id'], '|', ancestor::x:ServiceCollection/x:OtherIdentifier[@type='DVB_frequency'], '|', @id, '|Name:', x:Name)" -n $FILENAME | perl -pe 's/Name:\n/Name:/g' | perl -pe 's/Name:\s*/Name:/g' | perl -pe 's/^\s*\n//' | perl -pe 's/Name://g' |perl -pe 's/srid:\/\/bds.tv\///g' |sort -k3,3 -n > $1.tmp4

  for LCN in $LCNS
  do
    echo "Headend id: " $HID "LCN: " $LCN "Detail ---------"
    cat $1.tmp4 | grep '|'$LCN'|'
  done
done

for num in `seq 4`
do
  if [ -e $1.tmp$num ]
then
  rm $1.tmp$num
fi
done

